﻿using System;
using System.Windows.Input;
using System.Linq;

namespace HobSoft.SoccerBet2014.Client.Configuration
{
    using Domain.Models.Core;
    using ViewModels;
    [Serializable]
    public class BettingKeySetting : ViewModelBase, IModel
    {
        [NonSerialized]
        private KeyConverter keyConverter = new KeyConverter();
        [NonSerialized]
        private KeyGestureConverter keyGestureConverter = new KeyGestureConverter();
        public BettingKeySetting(string name, Key key, ModifierKeys modifierKey)
            : this()
        {
            Name = name;
            Key = key;
            ModifierKey = modifierKey;
            if ((ModifierKey & ModifierKeys.None) != ModifierKeys.None)
                Gesture = new KeyGesture(key, modifierKey);
            else
                Gesture = null;
            if (Gesture == null)
                _hotKey = keyConverter.ConvertToInvariantString(key);
            else
                _hotKey = keyGestureConverter.ConvertToInvariantString(Gesture);
        }

        public BettingKeySetting()
        {
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set { PropertyValueChange(ref _name, value, "Name"); }
        }

        [field: NonSerialized]
        private KeyGesture _gesture;
        [field: NonSerialized]
        public KeyGesture Gesture
        {
            get { return _gesture; }
            set
            {
                PropertyValueChange(ref _gesture, value, "Gesture");
            }
        }

        private ModifierKeys _modifierKey;

        public ModifierKeys ModifierKey
        {
            get { return _modifierKey; }
            set { PropertyValueChange(ref _modifierKey, value, "ModifierKey"); }
        }

        private Key _key;
        public Key Key
        {
            get { return _key; }
            set { PropertyValueChange(ref _key, value, "Key"); }
        }

        private string _hotKey;

        public string HotKey
        {
            get { return _hotKey; }
            set
            {
                PropertyValueChange(ref _hotKey, value, "HotKey");
                if (string.IsNullOrEmpty(_hotKey))
                {
                    Key = Key.None;
                    ModifierKey = ModifierKeys.None;
                    Gesture = null;
                }
                else
                    ResolveGesture(_hotKey);
            }
        }

        private decimal _money;
        public decimal Money
        {
            get { return _money; }
            set { PropertyValueChange(ref _money, value, "Money"); }
        }

        private HotKeyCommand _hotKeyCommand;

        public HotKeyCommand HotKeyCommand
        {
            get { return _hotKeyCommand; }
            set { PropertyValueChange(ref _hotKeyCommand, value, "HotKeyCommand"); }
        }

        protected void ResolveGesture(string text)
        {
            if (keyConverter == null)
                keyConverter = new KeyConverter();
            if (keyGestureConverter == null)
                keyGestureConverter = new KeyGestureConverter();
            if (string.IsNullOrEmpty(text)) return;
            if (text.IndexOf('+') > -1)
            {
                Gesture = (KeyGesture)keyGestureConverter.ConvertFromInvariantString(text);
                Key = Gesture.Key;
                ModifierKey = Gesture.Modifiers;
            }
            else
            {
                Key = (Key)keyConverter.ConvertFromInvariantString(text);
                ModifierKey = ModifierKeys.None;
                Gesture = null;
            }
        }
    }

    public enum HotKeyCommand : byte
    {
        Betting,
        Focus
    }
}
